/************************************************************************
 * <br>
 * <ul>
 * <li>版权所有：唐山弘川科技有限公司 2009－2020</li>
 * <li>创建日期：2009-8-10</li>
 * <li>初始作者：刘川</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 ***********************************************************************/
package com.hy.enterprise.framework.lang.pair ;

/**
 * <ul>
 * <li>设计作者：刘川</li>
 * <li>设计日期：2009-8-10</li>
 * <li>设计时间：下午01:48:23</li>
 * <li>设计目的：表现成对值的类</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 */
public class Pair < U , V > implements KeyValuePairable < U , V >
{
	private static final long	serialVersionUID	=
	                                                  - 3669785125744344879L ;

	/**
	 * 
	 */
	private boolean	          useEquals ;

	/**
	 * 
	 */
	public U	              key ;

	/**
	 * 
	 */
	public V	              value ;

	/**
	 * 构造函数
	 * 
	 * @param key
	 * @param value
	 */
	public Pair (
	              U key ,
	              V value )
	{
		this.key =
		           key ;
		this.value =
		             value ;
		this.useEquals =
		                 true ;
	}

	/**
	 * 构造函数
	 * 
	 * @param key
	 * @param value
	 * @param useEquals
	 */
	public Pair (
	              U key ,
	              V value ,
	              boolean useEquals )
	{
		this.key =
		           key ;
		this.value =
		             value ;
		this.useEquals =
		                 useEquals ;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@ Override
	@ SuppressWarnings ( "unchecked" )
	public boolean equals (
	                        Object object )
	{
		if ( this == object )
		{
			return true ;
		}
		if ( object == null )
		{
			return false ;
		}
		if ( this
		         .getClass ( ) != object
		                                .getClass ( ) )
		{
			return false ;
		}
		Pair < U , V > otherPair =
		                           ( Pair < U , V > ) object ;
		if ( this.key == null )
		{
			if ( otherPair.key != null )
			{
				return false ;
			}
		}
		else if ( this.useEquals
		                        ? ! this.key
		                                    .equals ( otherPair.key )
		                        : this.key != otherPair.key )
		{
			return false ;
		}
		if ( this.value == null )
		{
			if ( otherPair.value != null )
			{
				return false ;
			}
		}
		else if ( this.useEquals
		                        ? ! this.value
		                                      .equals ( otherPair.value )
		                        : this.value != otherPair.value )
		{
			return false ;
		}
		return true ;
	}

	/**
	 * @see com.vsoft.libra.enterprise.framework.lang.pair.KeyValuePairable#getKey()
	 */
	@ Override
	public U getKey ( )
	{
		return this.key ;
	}

	/**
	 * @see com.vsoft.libra.enterprise.framework.lang.pair.KeyValuePairable#getValue()
	 */
	@ Override
	public V getValue ( )
	{
		return this.value ;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@ Override
	public int hashCode ( )
	{
		final int prime =
		                  31 ;
		int result =
		             1 ;
		result =
		         prime
		                 * result
		                 + ( ( this.key == null )
		                                         ? 0
		                                         : this.key
		                                                   .hashCode ( ) ) ;
		result =
		         prime
		                 * result
		                 + ( ( this.value == null )
		                                           ? 0
		                                           : this.value
		                                                       .hashCode ( ) ) ;
		return result ;
	}

	/**
	 * @return the useEquals
	 */
	public boolean isUseEquals ( )
	{
		return this.useEquals ;
	}

	/**
	 * @see com.vsoft.libra.enterprise.framework.lang.pair.KeyValuePairable#setKey(java.lang.Object)
	 */
	@ Override
	public void setKey (
	                     U key )
	{
		this.key =
		           key ;
	}

	/**
	 * @param useEquals
	 *            the useEquals to set
	 */
	public void setUseEquals (
	                           boolean useEquals )
	{
		this.useEquals =
		                 useEquals ;
	}

	/**
	 * @see com.vsoft.libra.enterprise.framework.lang.pair.KeyValuePairable#setValue(java.lang.Object)
	 */
	@ Override
	public void setValue (
	                       V value )
	{
		this.value =
		             value ;
	}
}
